package com.teach.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.teach.commons.FileUtil;
import com.teach.commons.Id;
import com.teach.dto.LoginUser;
import com.teach.entity.Product;
import com.teach.exception.CodeException;
import com.teach.mapper.ProductMapper;
import com.teach.service.FileService;
import com.teach.service.ShopProductService;

@Service
public class ShopProductServiceImpl implements ShopProductService {

	@Value("${root:d:/upload}")
	private String root;

	@Autowired
	private Id id;

	@Autowired
	ProductMapper mapper;
	
	@Autowired
	FileService fileService;

	@Override
	public void updateProduct(Product form, MultipartFile img) {
		Long ids = id.nextId();
		/*
		String uri = "photo/" + new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + ids + "/";
		File path = new File(root, uri);
		path.mkdirs();

		// 保存原始图片
		File file = new File(path, "1." + FileUtil.getFileExt(img.getOriginalFilename()));
		System.out.println(img.getOriginalFilename());
		try {
			img.transferTo(file);
		} catch (Exception e) {
			throw new CodeException(100);
		}
		*/
		// 记录上传的相片信息
		Product p = new Product();
		BeanUtils.copyProperties(form, p);
		p.setProductManager(new LoginUser().getUserName());
		//p.setProductImgs(uri +"1." + FileUtil.getFileExt(img.getOriginalFilename()));
		p.setProductImgs(fileService.save(img, true));
		//System.out.println(p);
		mapper.updateByPrimaryKeySelective(p);
		/*
		 *  if (StringUtils.isEmpty(p.getPhotoName())) {
		 * p.setPhotoName(img.getOriginalFilename()); }
		 */
		
	}

	@Override
	public void addProduct(Product form, MultipartFile img) {
		Long ids = id.nextId();
		/*
		String uri = "photo/" + new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + ids + "/";
		File path = new File(root, uri);
		path.mkdirs();

		// 保存原始图片
		
		File file = new File(path, "1." + FileUtil.getFileExt(img.getOriginalFilename()));
		System.out.println(img.getOriginalFilename());
		try {
			img.transferTo(file);
		} catch (Exception e) {
			throw new CodeException(100);
		}
		*/
		// 记录上传的相片信息
		Product p = new Product();
		BeanUtils.copyProperties(form, p);
		p.setProductId(ids);
		p.setProductManager(new LoginUser().getUserName());
		p.setProductTime(new Date());
		p.setProductCreateTime(new Date());
		p.setProductSales(0);
		p.setProductOut(false);
		p.setProductIp(UUID.randomUUID().toString());
		p.setProductColors("无选择");
		//p.setProductImgs(uri+"1." + FileUtil.getFileExt(img.getOriginalFilename()));
		p.setProductImgs(fileService.save(img, true));
		System.out.println(p);
		mapper.insertSelective(p);
	}

}
